//
//  MyTextView.m
//  test-TextView
//
//  Created by 杨岑 on 2/3/15.
//  Copyright (c) 2015 杨岑. All rights reserved.
//

#import "MyTextView.h"

@implementation MyTextView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setup
{
    self.postsFrameChangedNotifications = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(frameDidChange:) name:NSViewFrameDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editEnd:) name:NSTextStorageDidProcessEditingNotification object:nil];
}

- (void)frameDidChange:(NSNotification*)notification
{
    if (notification.object == self)
    {
        NSLog(@"%@: frameDidChange", self);
        [self performSelector:@selector(onFrameChange:) withObject:nil afterDelay:.1];
    }
}

- (void)editEnd:(NSNotification*)notification
{
    //NSRect rect = [self.layoutManager usedRectForTextContainer:self.textContainer];
    //NSRect rect = self.bounds;
    //NSLog(@"editEnd: %.1f,%1.f", rect.size.width, rect.size.height);
}

- (void)onFrameChange:(id)o
{
    static NSSize oldSize;
    
    NSSize newSize = self.bounds.size;
    if (!NSEqualSizes(oldSize, newSize))
    {
        oldSize = newSize;
        if (self.onFrameChange) self.onFrameChange(self.frame);
    }
}

- (void)interpretKeyEvents:(NSArray *)eventArray
{
    //NSEvent* event = eventArray[0];
    NSLog(@"%@", eventArray);
    return [super interpretKeyEvents:eventArray];
}

- (NSSize)computedContentSize:(CGFloat)width
{
    //NSRect rect1 = [self.layoutManager usedRectForTextContainer:self.textContainer];
    
    //NSRange range = [self.layoutManager glyphRangeForTextContainer:self.textContainer];
    //NSRect rect2 = [self.layoutManager boundingRectForGlyphRange:range inTextContainer:self.textContainer];
    
    NSSize size = NSMakeSize(width, CGFLOAT_MAX);
    NSUInteger options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    NSRect rect3 = [self.textStorage boundingRectWithSize:size options:options];
    
    return rect3.size;
}

- (void)doCommandBySelector:(SEL)aSelector
{
    if ([NSStringFromSelector(aSelector) isEqualToString:@"noop:"])
    {
        NSEvent* event = [[NSApplication sharedApplication] currentEvent];
        if (event.modifierFlags & NSEventModifierFlagCommand)
        {
            if ([event.charactersIgnoringModifiers isEqualToString:@"a"])
            {
                [self selectAll:self];
            }
            else if ([event.charactersIgnoringModifiers isEqualToString:@"c"])
            {
                [self copy:self];
            }
            else if ([event.charactersIgnoringModifiers isEqualToString:@"f"])
            {
                NSMenuItem* menuItem = [[NSMenuItem alloc] init];
                menuItem.tag = NSTextFinderActionShowFindInterface;
                [self performFindPanelAction:menuItem];
            }
            else if ([event.charactersIgnoringModifiers isEqualToString:@"j"])
            {
                [self centerSelectionInVisibleArea:self];
            }
        }
    }
    else return [super doCommandBySelector:aSelector];
}
@end
